/*
 *
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: SocketResponseHandler.java
 *   @Date: 11/29/18 3:21 PM
 *   @Author: chenjun
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 *
 */

package io.bhex.sdk.socket;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import io.bhex.baselib.network.response.ResponseListener;

public class SocketResponseHandler<T> implements ResponseListener<T> {
    protected static final int SUCCESS_MESSAGE = 0;
    protected static final int FAILURE_MESSAGE = 1;
    protected static final int START_MESSAGE = 2;
    protected static final int FINISH_MESSAGE = 3;
    protected static final int NONET_MESSAGE = 4;

    protected static final int SUCCESS_OBJECTMESSAGE = 6;
    private Handler handler;

    public SocketResponseHandler() {
        if(Looper.myLooper() != null) {
            handler = new Handler(){
                public void handleMessage(Message msg){
                    SocketResponseHandler.this.handleMessage(msg);
                }
            };
        }
    }


    public  void handleMessage(Message msg) {
        Object[] response = null;

        switch(msg.what) {
            case SUCCESS_MESSAGE:
                response = (Object[])msg.obj;
                onSuccess((String)response[0]);
                break;
            case SUCCESS_OBJECTMESSAGE:
                response = (Object[])msg.obj;
                onSuccess((T)response[0]);
                break;
            case FAILURE_MESSAGE:
                response = (Object[])msg.obj;
                onFailed((String)response[0]);
                break;
            case START_MESSAGE:
                onBefore();
                break;
            case NONET_MESSAGE:
                onNoNet();
                break;
            case FINISH_MESSAGE:
                onAfter();
                onFinish();
                break;
        }

    }

    public void onCustomRes(T response){}



    @Override
    public void onSuccess(T response){}

    @Override
    public void onError(Throwable error) {

    }

    @Override
    public void onAfter() {

    }

    @Override
    public T parserResponse(Handler uiHandler, String response, Class<T> clazz) {
        return null;
    }

    public void onSuccess(String response){}



    public void onFailed(String error){};


    @Override
    public void onBefore() {

    }

    @Override
    public void onFinish() {

    }


    public void onNoNet() {}

    public void sendSuccessMessage(String responseBody) {
        sendMessage(obtainMessage(SUCCESS_MESSAGE, new Object[]{ responseBody}));
    }

    public void sendSuccessMessage(T Object) {
        sendMessage(obtainMessage(SUCCESS_OBJECTMESSAGE, new Object[]{Object}));
    }
    public void sendErrorMessage(String error) {
        sendMessage(obtainMessage(FAILURE_MESSAGE, new Object[]{error}));
    }

    protected void sendStartMessage() {
        sendMessage(obtainMessage(START_MESSAGE, null));
    }

    protected void sendFinishMessage() {
        sendMessage(obtainMessage(FINISH_MESSAGE, null));
    }

    protected void sendNoNetMessage() {
        sendMessage(obtainMessage(NONET_MESSAGE, null));
    }

    protected void sendMessage(Message msg) {
        if(handler != null){
            handler.sendMessage(msg);
        } else {
            handleMessage(msg);
        }
    }

    protected Message obtainMessage(int responseMessage, Object[] response) {
        Message msg = null;
        if(handler != null){
            msg = this.handler.obtainMessage(responseMessage, response);
        }else{
            msg = new Message();
            msg.what = responseMessage;
            msg.obj = response;
        }
        return msg;
    }

}


